package org.javaboy.web.controller.ai_interview;

import org.javaboy.ai.domain.*;
import org.javaboy.ai.service.*;
import org.javaboy.common.annotation.RateLimiter;
import org.javaboy.common.core.controller.BaseController;
import org.javaboy.common.core.domain.AjaxResult;
import org.javaboy.common.enums.LimitType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这个 Controller 专门用来处理和小程序相关的请求
 */
@RestController
@RequestMapping("/mp")
public class MpController extends BaseController {

    @Autowired
    IWxLoginService wxLoginService;
    @Autowired
    ICategoryService categoryService;
    @Autowired
    IBannerService bannerService;

    @Autowired
    IAiService aiService;
    @Autowired
    IModelsService modelsService;
    @Autowired
    ICategoryItemService categoryItemService;
    @Autowired
    IInterviewRecordsService interviewRecordsService;

    @GetMapping("/banners")
    public List<Banner> getAllBanners() {
        return bannerService.getAllBanners();
    }

    @GetMapping("/categories")
    public List<Category> getAllCategories() {
        return categoryService.getAllCategories();
    }

    @PostMapping("/login")
    public AjaxResult login(String code) {
        WxLogin wxLogin = wxLoginService.login(code);
        if (wxLogin.getOpenid() != null && wxLogin.getOpenid().length() > 0) {
            return success(wxLogin);
        } else {
            return AjaxResult.error(401, "登录失败");
        }
    }

    @PostMapping("/chat")
    @RateLimiter(key = "mp-chat", time = 3, count = 1, limitType = LimitType.IDENTITY)
    public MpAnswer chat(@RequestBody MpRequest mpRequest, @RequestHeader("openid") String openid) {
        return aiService.chat(mpRequest, openid);
    }

    @GetMapping("/models")
    public List<Models> getAllModels() {
        return modelsService.getAllModels();
    }

    @GetMapping("/categories/{category}")
    public List<CategoryItem> getCategoryItems(@PathVariable String category) {
        return categoryItemService.getCategoryItems(category);
    }

    @GetMapping("/records")
    public List<ChatRecords> getChatRecordsByPage(@RequestParam String subject,@RequestHeader("openid") String openid, @RequestParam(defaultValue = "1") int pageNum) {
        return interviewRecordsService.getChatRecordsByPage(subject,openid,pageNum);
    }
}
